package br.com.massai.model.entity;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PostLoad;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "CATEGORY_LEVEL")
@NamedQueries({
	@NamedQuery(name = "CategoryLevel.find", query = "SELECT c FROM CategoryLevel c")
})
public class CategoryLevel extends GenericEntity {

	private static final long serialVersionUID = -5617040998973097604L;
	public static final Long CATEGORY_MISCELLANEOUS = 11L;

	@Id
	@SequenceGenerator(name = "seqCategoryLevel", sequenceName = "seq_category_level", allocationSize = 1)
	@GeneratedValue(generator = "seqCategoryLevel", strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(name = "CODE", length = 2, columnDefinition = "VARCHAR(2)")
	private String code;

	@Column(name = "NAME_CATEGORY", length = 100, nullable = false, columnDefinition = "VARCHAR(100)")
	private String nameCategory;
	
	@Transient
	private Integer codeInt;
	
	@SuppressWarnings("unused")
	@PostLoad
	private void initCodes(){
		this.codeInt = Integer.valueOf(this.code);
	}

	@Override
	public Map<String, Object> getParameters() {
		if(this.code != null && !this.code.isEmpty()){
			super.addClauseWhere("c.code =:code", ConditionType.AND);
			super.addParameters("code", this.code);
		}

		if(this.nameCategory != null && !this.nameCategory.isEmpty()){
			super.addClauseWhere("LOWER(c.nameCategory) LIKE LOWER(:nameCategory)", ConditionType.AND);
			super.addParameters("nameCategory", "%" + this.nameCategory + "%");
		}

		return super.getParameters();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;

		if(obj instanceof CategoryLevel)
			if(((CategoryLevel)obj).getId().equals(this.getId())) return true;

		return false;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getNameCategory() {
		return nameCategory;
	}

	public void setNameCategory(String nameCategory) {
		this.nameCategory = nameCategory;
	}

	public Integer getCodeInt() {
		return codeInt;
	}

	public void setCodeInt(Integer codeInt) {
		this.codeInt = codeInt;
	}
}
